﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using System.Threading;
using MainProgram.ModelReport;
using UserAuthority;





namespace MainProgram
{
    public partial class FormMain : Form
    {
        ReportDocument myReport = new ReportDocument();
        DataSetReport ds = new DataSetReport();

        SqlController mySqlController = new SqlController();

        private bool loginState;
        private User user = new User();

        public void SetUser(string name,int authority)
        {
            user.UserName = name;
            user.UserAuthority = authority;
        }
        

        enum DataState
        {
            Normal,
            Warning,
            Alert,
        };



        public bool LoginState
        {
            get { return loginState; }
            set { loginState = value; }
        }
        

      
        public FormMain()
        {
            InitializeComponent();
            loginState = false;
            user.AuthorityChanged+=new User.AuthorityEventHandler(OnAuthorityChanged);


                
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormLogin myFormLogin = new FormLogin(this);
            myFormLogin.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mySqlController.Close();
            Application.Exit();
        }

        private void OnAuthorityChanged(object sender,EventArgs e)
        {
            //if (mySqlController.GetState() != ConnectionState.Open && loginState==true)
            //{
            //    string strConnection = @"Data Source=.\SQLEXPRESS;Initial Catalog=Monitor;User ID=root;Password=123";
            //    mySqlController.SetConnectionString(strConnection);
            //    mySqlController.Open();
            //}
            //else if (mySqlController.GetState()!=ConnectionState.Closed && loginState==false)
            //{
            //    mySqlController.Close();
            //}

            if (user.UserAuthority != 0)
            {
                labelItem3.Text = user.UserName+" 已登录";
            }
            else
            {
                labelItem3.Text = "未登录";
            }

        }



        private void buttonItem1_Click(object sender, EventArgs e)
        {
            if (true)
            {
                FormRealTime subForm = new FormRealTime();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem16_Click(object sender, EventArgs e)
        {
            if (true)
            {
                FormDataHistory subForm = new FormDataHistory();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem18_Click(object sender, EventArgs e)
        {
            if (true)
            {
                FormControlHistory subForm = new FormControlHistory();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem17_Click(object sender, EventArgs e)
        {
            if (true)
            {
                FormNodeInfo subForm = new FormNodeInfo();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem24_Click(object sender, EventArgs e)
        {
            if (true)
            {
                FormAutoSendCommand subForm = new FormAutoSendCommand();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem25_Click(object sender, EventArgs e)
        {
            if (true)
            {
                FormManualSendCommand subForm = new FormManualSendCommand();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem19_Click(object sender, EventArgs e)
        {
            if (true)
            {
                FormSetLevel subForm = new FormSetLevel();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem18_Click_1(object sender, EventArgs e)
        {
            if (true)
            {
                FormControlHistory subForm = new FormControlHistory();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem26_Click(object sender, EventArgs e)
        {
            if (true)
            {
                FormGIS subForm = new FormGIS();
                subForm.TopLevel = false;
                panelSubForm.Controls.Clear();
                panelSubForm.Controls.Add(subForm);
                subForm.Show();
            }
        }

        private void buttonItem14_Click(object sender, EventArgs e)
        {
            if (user.UserAuthority==0)
            {
                FormLogin subForm = new FormLogin(this);
                subForm.ShowDialog();
            }
            else
            {
                MessageBox.Show("请先注销");
            }
        }

        private void buttonItem15_Click(object sender, EventArgs e)
        {
            if (user.UserAuthority == 0)
            {
                MessageBox.Show("未登录");
            }
            else
            {
                user.SetUser(null, 0);
            }

        }






    }
}
